using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using System.Windows.Forms;
using LeisureCenter.Api;

namespace Aspire
{
    public enum AspireKnobCode
    {
        None = 0,
        PlayPause = 0xB0,
        StopEject,
        Previous,
        Next,
        VolumeDown,
        VolumeUp,
        Locked = 0xB7
    }

    public class AspireKnobControl
    {
        private AspirePlugin plugin;
        SerialPort serialPort;

        public AspireKnobControl(AspirePlugin plugin)
        {
            this.plugin = plugin;
            this.serialPort = plugin.serialPort;
            serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
        }

        void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (e.EventType == SerialData.Chars)
            {
                switch ((AspireKnobCode)serialPort.ReadByte())
                {
                    case AspireKnobCode.PlayPause:
                        plugin.leisureCenterInstance.CommandsManager.EnqueueCommand(LeisureCenterCommand.PlayPause);
                        break;
                    case AspireKnobCode.StopEject:
                        plugin.leisureCenterInstance.CommandsManager.EnqueueCommand(LeisureCenterCommand.StopEject);
                        break;
                    case AspireKnobCode.Previous:
                        plugin.leisureCenterInstance.CommandsManager.EnqueueCommand(LeisureCenterCommand.Previous);
                        break;
                    case AspireKnobCode.Next:
                        plugin.leisureCenterInstance.CommandsManager.EnqueueCommand(LeisureCenterCommand.Next);
                        break;
                    case AspireKnobCode.VolumeDown:
                        plugin.leisureCenterInstance.CommandsManager.EnqueueCommand(LeisureCenterCommand.VolumeDown);
                        break;
                    case AspireKnobCode.VolumeUp:
                        plugin.leisureCenterInstance.CommandsManager.EnqueueCommand(LeisureCenterCommand.VolumeUp);
                        break;
                    case AspireKnobCode.Locked:
                        break;
                }
            }
        }
    }
}
